********************************************************************************
*   Project: The Diabetes care continuum in Venezuela						   *
*   Task: Table A1: Decriptive stats for baseline only population 			   *
*	Author: Dina Goodman (Harvard University)      							   *
*	Code check:  				  											   *
*   Date: 29 Sept 2021													 	   *
*	Last updated:							   				   				   *
********************************************************************************
*Set globals for entire anaylsis
global Data "/Users/dinagoodman/Dropbox/Harvard/Dissertation/Paper 3/Data"
global Results "/Users/dinagoodman/Dropbox/Harvard/Dissertation/Paper 3/Results"
global Code "/Users/dinagoodman/Dropbox/Harvard/Dissertation/Paper 3/Code"
cd "$Data"
use "Paper 3_Final BL Dataset_8.12.21.dta", clear

/*Table 1 Code*/
svyset Parroquia [pweight = weighted_age_sex_region]

*Set global
global vars agecat ses urban overweightobese htn highLDL
*check for ps - exact if any cells <=5
foreach var in $vars {
svy, subpop(if clindia_self==1): tab `var' Gender, col pearson
}
//all ps <0.001 except agwork
count if clindia_self==1
count if clindia_self==1 & Gender==1
count if clindia_self==1 & Gender==2

*Add headers
	cd "$Results"
capture rm "tablea1_output.xls"
putexcel set "tablea1_output.xls", replace
	
*Column Headers
	putexcel B1=("Total")
	putexcel B2=("%")
	putexcel C2=("LB")
	putexcel D2=("UB")	
	putexcel E1=("Male")
	putexcel E2=("%")
	putexcel F2=("LB")
	putexcel G2=("UB")	
	putexcel H1=("Female")
	putexcel H2=("%")
	putexcel I2=("LB")
	putexcel J2=("UB")
*Row headers
	
mat tab1=J(1,6,0) 
local col1=1
****Overall by Gender
svy, subpop(if clindia_self==1): prop Gender
	mat rtable = r(table)
	mat tab1[1,`col1']= rtable[1,1]*100  //% no dm
		local ++col1
	mat tab1[1,`col1']= rtable[5,1]*100 //lb no dm
		local ++col1
	mat tab1[1,`col1'] = rtable[6,1]*100 //ub no dm
		local ++col1
	mat tab1[1,`col1']= rtable[1,2]*100 //% dm
		local ++col1
	mat tab1[1,`col1']= rtable[5,2]*100 //lb dm
		local ++col1
	mat tab1[1,`col1'] = rtable[6,2]*100 //ub dm
		local ++col1	
putexcel E3=matrix(tab1), nformat(#.0)

****All covariates

*****Age category
putexcel A4=("Age category") 
local row=5

	foreach val in 1 2 3 { 
		foreach var in age3cat {
svy, subpop(if clindia_self==1): prop `var'
		mat rtable = r(table) 
			mat `var'_tprop = rtable[1,`val']*100
			mat `var'_tlb = rtable[5,`val']*100
			mat `var'_tub = rtable[6,`val']*100

*for no DM
	svy, subpop(if clindia_self==1 & Gender==1): prop `var'
		mat rtable = r(table) 
			mat `var'_0prop = rtable[1,`val']*100
			mat `var'_0lb = rtable[5,`val']*100
			mat `var'_0ub = rtable[6,`val']*100
		
*for yes DM
	svy, subpop(if clindia_self==1 & Gender==2): prop `var'
		mat rtable = r(table) 
			mat `var'_1prop = rtable[1,`val']*100
			mat `var'_1lb = rtable[5,`val']*100
			mat `var'_1ub = rtable[6,`val']*100


putexcel B`row'=matrix(`var'_tprop), nformat(#.0)
putexcel C`row'=matrix(`var'_tlb), nformat("(0.0;(-#.0")  
putexcel D`row'=matrix(`var'_tub), nformat(", 0.0);, -0.0) ") 
putexcel E`row'=matrix(`var'_0prop), nformat(#.0)
putexcel F`row'=matrix(`var'_0lb), nformat("(0.0;(-#.0")  
putexcel G`row'=matrix(`var'_0ub), nformat(", 0.0);, -0.0) ") 
putexcel H`row'=matrix(`var'_1prop), nformat(#.0)	
putexcel I`row'=matrix(`var'_1lb), nformat("(0.0;(-#.0")  
putexcel J`row'=matrix(`var'_1ub), nformat (", 0.0);, -0.0) ") 
	
		}
		local ++row
	}
di "row is `row'"


*****Wealth quintile
putexcel A8=("Wealth Quintile") 
local row=9 

	foreach val in 1 2 3 { 
		foreach var in ses {
svy, subpop(if clindia_self==1): prop `var'
		mat rtable = r(table) 
			mat `var'_tprop = rtable[1,`val']*100
			mat `var'_tlb = rtable[5,`val']*100
			mat `var'_tub = rtable[6,`val']*100

*for no DM
	svy, subpop(if clindia_self==1 & Gender==1): prop `var'
		mat rtable = r(table) 
			mat `var'_0prop = rtable[1,`val']*100
			mat `var'_0lb = rtable[5,`val']*100
			mat `var'_0ub = rtable[6,`val']*100
		
*for yes DM
	svy, subpop(if clindia_self==1 & Gender==2): prop `var'
		mat rtable = r(table) 
			mat `var'_1prop = rtable[1,`val']*100
			mat `var'_1lb = rtable[5,`val']*100
			mat `var'_1ub = rtable[6,`val']*100


putexcel B`row'=matrix(`var'_tprop), nformat(#.0)
putexcel C`row'=matrix(`var'_tlb), nformat("(0.0;(-#.0")  
putexcel D`row'=matrix(`var'_tub), nformat(", 0.0);, -0.0) ") 
putexcel E`row'=matrix(`var'_0prop), nformat(#.0)
putexcel F`row'=matrix(`var'_0lb), nformat("(0.0;(-#.0")  
putexcel G`row'=matrix(`var'_0ub), nformat(", 0.0);, -0.0) ") 
putexcel H`row'=matrix(`var'_1prop), nformat(#.0)	
putexcel I`row'=matrix(`var'_1lb), nformat("(0.0;(-#.0")  
putexcel J`row'=matrix(`var'_1ub), nformat (", 0.0);, -0.0) ") 
		}
		local ++row
	}
di "row is `row'"


***Urban
putexcel A12=("Urban")
	foreach var in urban {
		svy, subpop(if clindia_self==1): prop `var'
		mat rtable = r(table) //store prop results in a matrix call rtable
			mat `var' =J(1,9,0) //create a new matrix for this variable
			local col1=1 //setting initial column
			mat `var'[1,`col1']= rtable[1,2]*100 //total % 
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 //lb total
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100 //ub total
		local ++col1	

*for no DM
		svy, subpop(if clin_dia==1 & Gender==1): prop `var'
			mat rtable = r(table)
			mat `var'[1,`col1']= rtable[1,2]*100
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100
		local ++col1
		
*for yes DM
		svy, subpop(if clin_dia==1 & Gender==2): prop `var'
			mat rtable = r(table)
			mat `var'[1,`col1']= rtable[1,2]*100
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100
 putexcel B12=matrix(`var'), nformat(#.0)	
			}
			
***Obese
putexcel A13=("BMI >25")
	foreach var in overweightobese {
		svy, subpop(if clindia_self==1): prop `var'
		mat rtable = r(table) //store prop results in a matrix call rtable
			mat `var' =J(1,9,0) //create a new matrix for this variable
			local col1=1 //setting initial column
			mat `var'[1,`col1']= rtable[1,2]*100 //total % 
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 //lb total
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100 //ub total
		local ++col1	

*for no DM
		svy, subpop(if clin_dia==1 & Gender==1): prop `var'
			mat rtable = r(table)
			mat `var'[1,`col1']= rtable[1,2]*100
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100
		local ++col1
		
*for yes DM
		svy, subpop(if clin_dia==1 & Gender==2): prop `var'
			mat rtable = r(table)
			mat `var'[1,`col1']= rtable[1,2]*100
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100
 putexcel B13=matrix(`var'), nformat(#.0)	
			}
				
***Hypertension 
putexcel A14=("Hypertension")
	foreach var in htn {
		svy, subpop(if clindia_self==1): prop `var'
		mat rtable = r(table) //store prop results in a matrix call rtable
			mat `var' =J(1,9,0) //create a new matrix for this variable
			local col1=1 //setting initial column
			mat `var'[1,`col1']= rtable[1,2]*100 //total % 
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 //lb total
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100 //ub total
		local ++col1	

*for no DM
		svy, subpop(if clin_dia==1 & Gender==1): prop `var'
			mat rtable = r(table)
			mat `var'[1,`col1']= rtable[1,2]*100
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100
		local ++col1
		
*for yes DM
		svy, subpop(if clin_dia==1 & Gender==2): prop `var'
			mat rtable = r(table)
			mat `var'[1,`col1']= rtable[1,2]*100
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100
 putexcel B14=matrix(`var'), nformat(#.0)	
			}
								
***High LDL 
putexcel A15=("High LDL")
	foreach var in highLDL {
		svy, subpop(if clindia_self==1): prop `var'
		mat rtable = r(table) //store prop results in a matrix call rtable
			mat `var' =J(1,9,0) //create a new matrix for this variable
			local col1=1 //setting initial column
			mat `var'[1,`col1']= rtable[1,2]*100 //total % 
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 //lb total
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100 //ub total
		local ++col1	

*for no DM
		svy, subpop(if clin_dia==1 & Gender==1): prop `var'
			mat rtable = r(table)
			mat `var'[1,`col1']= rtable[1,2]*100
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100
		local ++col1
		
*for yes DM
		svy, subpop(if clin_dia==1 & Gender==2): prop `var'
			mat rtable = r(table)
			mat `var'[1,`col1']= rtable[1,2]*100
		local ++col1	
			mat `var'[1,`col1']= rtable[5,2]*100 
		local ++col1
			mat `var'[1,`col1'] = rtable[6,2]*100
 putexcel B15=matrix(`var'), nformat(#.0)	
			}
